package net.notelife.server;
import com.sun.net.httpserver.HttpServer;
import java.net.InetSocketAddress;
import java.util.Properties;

import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.Headers;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.File;
import java.io.IOException;
public class MyHttpServer
{
	private static String root = null;
	public MyHttpServer() throws Exception
	{
		Properties prop = new Properties();
		InputStream in = this.getClass().getResourceAsStream("web.properties");
		try
		{
			prop.load(in);
			root = prop.getProperty("www",null);
		}
		catch(Exception e)
		{
			System.err.println("load inf file error");
			throw e;
		}
	}
	public static void main(String[] args)
	{
		String hostname = "127.0.0.1";
		int port = 8080;
		int backlog = 1000;
		try
		{
			HttpServer httpserver = HttpServer.create();
			MyHttpServer myserver = new MyHttpServer();
			InetSocketAddress addr = new InetSocketAddress(hostname,port);
			httpserver.bind(addr,backlog);
			String path = "/";
			httpserver.createContext(path,new HtmlHandler(myserver.root,"",path.length()));
			path = "/images/05/";
			httpserver.createContext(path,new JpgHandler(myserver.root+File.separator+"05晋书","05晋书",path.length()));
			httpserver.start();
		}
		catch(IOException e)
		{
			System.out.println(e.toString());
		}
		catch(Exception e)
		{
			System.out.println(e.toString());
		}
		

	}
}
